package ca.etsmtl.log660.controller;

import ca.etsmtl.log660.dao.EntityFacade;
import ca.etsmtl.log660.entity.Client;

public class ClientManager {

	private static ClientManager instance = null;
	
	private Client loggedInClient = null;
	private EntityFacade entityFacade;
	
	private ClientManager() {
		this.entityFacade = new EntityFacade();
	}
	
	public static ClientManager getInstance() {
		if (instance == null) {
			instance = new ClientManager();
		}
		return instance;
	}
	
	public boolean logUserIn(String email, String password) {
		boolean isUserLoggedIn = false;
		Client client = this.entityFacade.getUserByCredential(email, password);
		
		if (client != null) {
			isUserLoggedIn = true;
			this.loggedInClient = client;
		}
		
		return isUserLoggedIn;
	}
	
	public Client getLoggedInClient() {
		return this.loggedInClient;
	}
	
	public Client getLoggedInClient(boolean useCache) {
		if (useCache) {
			return this.getLoggedInClient();
		} else {
			Long idClient = this.loggedInClient.getIdClient();
			Client updatedClient = this.entityFacade.getClientById(idClient);
			if (updatedClient != null) {
				this.loggedInClient = updatedClient;
			}
			return this.loggedInClient;
		}
	}
}
